const int inf = 2e9 + 5;
void solve()
{
  int n, m;
  cin >> n >> m;

  vi f(1 << 9), c(1 << 9);
  rep(i, 1, n) {
    int mask = 0, k, x;
    cin >> k;
    rep(j, 1, k) {
      cin >> x;
      mask |= 1 << (x - 1);
    }
    c[mask]++;
  }

  rep(i, 1, (1 << 9) - 1) {
    for (int j = i; j; j = ((j - 1) & i)) {
      f[i] += c[j];
    }
  }

  vi cost(m), msks(m);
  rep(i, 0, m - 1) {
    cin >> cost[i];
    int mask = 0, k, x;
    cin >> k;
    rep(j, 1, k) {
      cin >> x;
      mask |= 1 << (x - 1);
    }
    msks[i] = mask;
  }

  vi ord(m);
  iota(all(ord), 0);
  sort(all(ord), [&](int &x, int &y){
    return cost[x] < cost[y];
  });

  vvi goods(1 << 9);
  rep(i, 0, m - 1) {
    int j = ord[i];
    // debug(j, msks[j]);
    if (sz(goods[msks[j]]) < 2) {
      goods[msks[j]].pb(j);
    }
  }

  int ans1 = -inf, ans2 = inf;
  pair<int, int> idx;
  rep(l, 0, (1 << 9) - 1) {
    rep(r, 0, (1 << 9) - 1) {
      if (!sz(goods[l]) || !sz(goods[r])) {
        continue;
      }
      if (l == r && sz(goods[l]) < 2) {
        continue;
      }
      int i = goods[l].front(), j = goods[r].front();
      if (l == r) {
        j = goods[r].back();
      }
      if (ans1 < f[l | r]) {
        ans1 = f[l | r];
        ans2 = cost[i] + cost[j];
        idx = {i, j};
      } else if (ans1 == f[l | r] && ans2 > cost[i] + cost[j]) {
        ans2 = cost[i] + cost[j];
        idx = {i, j};
      }
    }
  }

  cout << idx.fi + 1 << ' ' << idx.se + 1 << '\n';
}